package com.pucong.gitee.database.backup.configuration.authorize;

import com.pucong.gitee.database.backup.common.entity.sys.authorize.AuthorizeInfo;
import com.pucong.gitee.database.backup.common.entity.sys.authorize.AuthorizeResult;
import com.pucong.gitee.database.backup.common.entity.sys.authorize.AuthorizeServerInfo;
import com.pucong.gitee.database.backup.utils.ApplicationContextUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

@Slf4j
public class LicenseAuth {

    public static AuthorizeResult check() {
        String path = ApplicationContextUtil.getSystemBasePath() + File.separator + "license" + File.separator + "licence";
        LicenseManager licenseManager = new LicenseManager(path);
        AuthorizeInfo license = licenseManager.getLicense();
        AuthorizeResult authorizeResult = allCheck(license);
        return authorizeResult;
    }

    public static AuthorizeInfo getLicenseObject() {
        String path = ApplicationContextUtil.getSystemBasePath() + File.separator + "license" + File.separator + "licence";
        log.info("获取认证文件路径：" + path);
        LicenseManager licenseManager = new LicenseManager(path);
        AuthorizeInfo license = licenseManager.getLicense();
        return license;
    }

    public static AuthorizeResult allCheck(AuthorizeInfo license) {
        AuthorizeResult authorizeResult = new AuthorizeResult();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(license.getExpireDate());
        String content = dateString + "," + license.getMac();
        String sign = RSASignature.sign(content, RSASignature.privateKey, RSASignature.ENCODE);
        boolean isCheckPass = false;
        try {
            isCheckPass = RSASignature.doCheck(content, new String(license.getSignature(), RSASignature.ENCODE), RSASignature.pubicKey, RSASignature.ENCODE);
        } catch (UnsupportedEncodingException e) {
            isCheckPass = false;
        }
        if(!isCheckPass){
            log.debug("KEY ERROR!");
            authorizeResult.setSuccess(false);
            authorizeResult.setErrorMsg("约签错误");
            return authorizeResult;
        }
        AuthorizeServerInfo localInetMac = NetworkUtils.getLocalInetMac();
        if(localInetMac == null){
            log.debug("MAC ERROR!");
            authorizeResult.setSuccess(false);
            authorizeResult.setErrorMsg("mac错误，请检查是否开启网卡启动");
            return authorizeResult;
        }
        log.info("本机信息：" + localInetMac.toString());
        // mac 校验
        if (!localInetMac.getMac().equals(license.getMac())) {
            log.debug("MAC ERROR!");
            authorizeResult.setSuccess(false);
            authorizeResult.setErrorMsg("mac错误");
            return authorizeResult;
        }
        // 有效时间校验
        if (license.isExpired()) {
            log.debug("EXPIRE DATE ERROR!");
            authorizeResult.setSuccess(false);
            authorizeResult.setErrorMsg("有效时间错误");
            return authorizeResult;
        }
        authorizeResult.setSuccess(true);
        authorizeResult.setErrorMsg("授权成功");
        return authorizeResult;
    }

    public static void main(String[] args) {
        AuthorizeInfo license = LicenseAuth.getLicenseObject();
        AuthorizeResult authorizeResult = allCheck(license);
        System.out.println(authorizeResult);
    }
}
